﻿using lwzlz.Application.System;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using lzwlz.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.Mall.Controllers
{
    public class CommodityController : MallController
    {
        #region 界面
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            var categoryList = new CategoryService().GetList();
            ViewData["categoryList"] = categoryList;
            return View();
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <returns></returns>
        public ActionResult Detail(int id)
        {
            var model = new CommodityService().Get(id);
            return View(model);
        }

        #endregion

        #region 接口
        /// <summary>
        /// 商城列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CommodityList(int pageNumber, int pageSize, int? categoryId = null, string keyword = null)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var commodityService = new CommodityService();
                Dictionary<string, string> dic = new Dictionary<string, string>();
                //分类
                if (categoryId.HasValue)
                {
                    dic.Add("CategoryId", categoryId.Value.ToString());
                }
                //关键词
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    dic.Add("Name", keyword);
                }

                var list = commodityService.GetListPage(pageNumber, pageSize, dic, null, null, out int count);

                response.Data = new { List = list, Count = count, };
                response.Success = true;
                response.Message = "获取成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="commodityId"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddShoppingCart(int commodityId, int number)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var commodity = new CommodityService().Get(commodityId);
                if (commodity != null && !commodity.IsDeleted)
                {
                    if (commodity.Number >= number)
                    {
                        var memberLogin = MallMemberLogin.GetCurrentUser();
                        var shoppingCartList = new ShoppingCartService().GetShoppingCartList(memberLogin.Id);
                        var ShoppingCart = shoppingCartList.Where(p => p.CommodityId == commodityId).FirstOrDefault();
                        if (ShoppingCart != null)
                        {
                            ShoppingCart.Number = number;
                            var result = new ShoppingCartService().Update(ShoppingCart, null);
                            if (result > 0)
                            {
                                response.Success = true;
                                response.Message = "修改成功";
                            }
                            else
                            {
                                throw new Exception("修改失败");
                            }
                        }
                        else
                        {
                            var model = new ShoppingCart()
                            {
                                MemberId = memberLogin.Id,
                                CommodityId = commodity.Id,
                                Number = number,
                                CreationTime = DateTime.Now,
                                IsDeleted = false
                            };
                            var result = new ShoppingCartService().Insert(model, null);
                            if (result.HasValue && result.Value > 0)
                            {
                                response.Success = true;
                                response.Message = "添加成功";
                            }
                            else
                            {
                                throw new Exception("添加失败");
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("库存量不足");
                    }
                }
                else
                {
                    throw new Exception("商品不存在");
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion
    }
}